package com.bxd.baodan.widget;


import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.util.GlideUtil;
import com.umeng.analytics.MobclickAgent;


public class IosAdDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout adLy;
    private ImageView adIv, adClose;
    private Display display;


    public IosAdDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public IosAdDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_ad_dialog, null);
        adLy = (LinearLayout) view.findViewById(R.id.dialog_ad_ly);
        adIv = (ImageView) view.findViewById(R.id.dialog_ad_iv);
        adClose = (ImageView) view.findViewById(R.id.dialog_ad_close);

        adClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小

        adLy.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));

        return this;
    }


    public IosAdDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public IosAdDialog setImage(String url) {

        GlideUtil.loadImage(context, url, adIv);
        return this;
    }
    public IosAdDialog setCloseClick(final View.OnClickListener listener) {

        adClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public IosAdDialog setAdClick(final View.OnClickListener listener) {

        adIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MobclickAgent.onEvent(App.getContext(), "v6_1_3_TC");
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }


    public void show() {
        setLayout();
        dialog.show();
    }

    private void setLayout() {
    }
}
